package in.iqing.control.helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.io.File;

import in.iqing.app.BuildConfig;
import in.iqing.app.R;

/**
 * Created by neavo on 2014/8/2.
 *  TODO rewrite or use Picasso directly
 */

public class ImageHelper {

    private Context ctx;
    private static ImageHelper singleton;

    public static final int TYPE_1x1 = 0x0001;
    public static final int TYPE_2x1 = 0x0002;
    public static final int TYPE_3x4 = 0x0003;

    private static final int DEFAULT_TYPE = TYPE_1x1;

    public ImageHelper(Context ctx) {
        this.ctx = ctx;
    }

    public static synchronized ImageHelper getSingleton(Context ctx) {
        if (singleton == null) {
            singleton = new ImageHelper(ctx);
        }

        return singleton;
    }

    public void onInitialize() {
        Picasso.with(ctx).setLoggingEnabled(BuildConfig.DEBUG);
        Picasso.with(ctx).setIndicatorsEnabled(BuildConfig.DEBUG);
    }

    public void load(Object target, ImageView iv) {
        load(target, iv, null);
    }

    public void load(Object target, ImageView iv, Callback callback) {
        load(DEFAULT_TYPE, target, iv, null, callback, -1, -1);
    }

    public void load(Object target, ImageView iv, Transformation transformation, Callback callback) {
        load(DEFAULT_TYPE, target, iv, transformation, callback, -1, -1);
    }

    public void load(Object target, ImageView iv, Transformation transformation, Callback callback
            , int centerCropResizeWidthRes, int centerCropResizeHeightRes) {
        load(DEFAULT_TYPE, target, iv, transformation, callback, -1, -1, centerCropResizeWidthRes, centerCropResizeHeightRes);
    }

    private void load(int type, Object target, ImageView iv, Transformation transformation, Callback callback, int holderRes, int errorRes) {
        load(type, target, iv, transformation, callback, holderRes, errorRes, -1, -1);
    }

    private void load(int type, Object target, ImageView iv, Transformation transformation, Callback callback, int holderRes, int errorRes
            , int centerCropResizeWidthRes, int centerCropResizeHeightRes) {
        RequestCreator creater;

        if (target instanceof String && !TextUtils.isEmpty((String) target)) {
            creater = Picasso.with(ctx).load((String) target);
        } else if (target instanceof File) {
            creater = Picasso.with(ctx).load((File) target);
        } else if (target instanceof Uri) {
            creater = Picasso.with(ctx).load((Uri) target);
        } else if (target instanceof Integer) {
            creater = Picasso.with(ctx).load((int) target);
        } else {
            return;
        }

        creater.config(Bitmap.Config.RGB_565);

        if (transformation != null) {
            creater.transform(transformation);
        }

        // TODO use different resource for different type
        switch (type) {
            case TYPE_1x1:
                creater = creater.error(errorRes < 0 ? R.drawable.image_error : errorRes);
                creater = creater.placeholder(holderRes < 0 ? R.drawable.image_loading_1x1 : holderRes);
                break;
            case TYPE_2x1:
                creater = creater.error(errorRes < 0 ? R.drawable.image_error : errorRes);
                creater = creater.placeholder(holderRes < 0 ? R.drawable.image_loading_1x1 : holderRes);
                break;
            case TYPE_3x4:
                creater = creater.error(errorRes < 0 ? R.drawable.image_error : errorRes);
                creater = creater.placeholder(holderRes < 0 ? R.drawable.image_loading_1x1 : holderRes);
                break;
        }

        // center crop with resize dimens
        if (centerCropResizeWidthRes != -1 && centerCropResizeHeightRes != -1) {
            creater.resizeDimen(centerCropResizeWidthRes, centerCropResizeHeightRes)
                    .centerCrop();
        }

        if (callback == null) {
            creater.into(iv);
        } else {
            creater.into(iv, callback);
        }
    }

}
